package org.dromara.third.convert.enterprise.validator;

import cn.dev33.satoken.annotation.SaIgnore;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.convert.enterprise.dto.UserDTO;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import java.util.List;

@Component
@SaIgnore
public class UserValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return List.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        List<UserDTO> userDTOS = (List<UserDTO>) target;

        // 基础字段验证
        validateRequiredFields(userDTOS, errors);
    }

    /**
     * 验证必填字段
     */
    private void validateRequiredFields(List<UserDTO> dtos, Errors errors) {
        dtos.forEach(dto -> {
            ValidateUtil.rejectIfNull(dto::getId, String.valueOf(ExceptionEnum.AuthException01.getCode()), "主键不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getXm, String.valueOf(ExceptionEnum.AuthException01.getCode()), "姓名不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getXb, String.valueOf(ExceptionEnum.AuthException01.getCode()), "性别不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getNl, String.valueOf(ExceptionEnum.AuthException01.getCode()), "年龄不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getSjh, String.valueOf(ExceptionEnum.AuthException01.getCode()), "手机号不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getSsbm, String.valueOf(ExceptionEnum.AuthException01.getCode()), "所属部门不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getGw, String.valueOf(ExceptionEnum.AuthException01.getCode()), "岗位不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getSfzhm, String.valueOf(ExceptionEnum.AuthException01.getCode()), "身份证号不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getRylx, String.valueOf(ExceptionEnum.AuthException01.getCode()), "人员类型不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getCsrq, String.valueOf(ExceptionEnum.AuthException01.getCode()), "出生日期不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getDelFlag, String.valueOf(ExceptionEnum.AuthException01.getCode()), "删除标志不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getCreateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建时间不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getUpdateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新时间不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getCreateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建人不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getUpdateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新人不能为空", errors);
        });
    }
}
